package com.lv.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lv.mapper.TbItemCatMapper;
import com.lv.mapper.TbSpecificationOptionMapper;
import com.lv.mapper.TbTypeTemplateMapper;
import com.lv.pojo.TbItemCat;
import com.lv.pojo.TbSpecificationOption;
import com.lv.pojo.TbSpecificationOptionExample;
import com.lv.pojo.TbTypeTemplate;
import com.lv.service.TypeService;
import com.lv.util.Page;
import com.lv.vo.SpecVO2;
import com.lv.vo.TemplateVO;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class TypeServiceImpl implements TypeService {
    @Resource
    private TbTypeTemplateMapper tm;

    @Resource
    private TbItemCatMapper cm;

    @Resource
    private TbSpecificationOptionMapper opm;

    @Override
    public Page<TbTypeTemplate> getInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbTypeTemplate> list =tm.selectByExample(null);
        PageInfo pb = new PageInfo(list);
        return new Page(pageNum,pageSize,pb.getTotal(),pb.getList(), pb.getPages());
    }

    //增加

    @Override
    public Integer add(TbTypeTemplate type) {
        return tm.insertSelective(type);
    }
    
    //删除

    @Override
    public void del(List<Long> ids) {
        for (long id:
             ids) {
            tm.deleteByPrimaryKey(id);
        }
    }

    //回显

    @Override
    public TbTypeTemplate toupdate(Long id) {
        return tm.selectByPrimaryKey(id);
    }

    @Override
    public void update(TbTypeTemplate type) {
        tm.updateByPrimaryKeySelective(type);
    }

    @Override
    public TbTypeTemplate selectById(Long typeId) {
        return tm.selectByPrimaryKey(typeId);
    }

    //根据分类的三级联动id查询模板数据

    @Override
    public TemplateVO getType(Long itemCatId)  {
        //根据分类的Id查询分类的对象
        TbItemCat cat = cm.selectByPrimaryKey(itemCatId);
        //取出模板Id
        Long typeId = cat.getTypeId();
        //根据模板Id查询模板对象
        TbTypeTemplate tbTypeTemplate = tm.selectByPrimaryKey(typeId);

        TemplateVO vo = new TemplateVO();
        //将tbTypeTemplate  cope复制到vo类当中
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        //从模板中取出规格的字符串
        String specJson = tbTypeTemplate.getSpecIds();
        //然后转换成集合
        List<SpecVO2> specVO2List = JSONObject.parseArray(specJson, SpecVO2.class);
        //根据VO的 Id 查看 option
        for (SpecVO2 specVO2 : specVO2List) {
            Long id = specVO2.getId();
            TbSpecificationOptionExample example = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
            criteria.andSpecIdEqualTo(id);
            List<TbSpecificationOption> optionsList = opm.selectByExample(example);
            //放入 SpecVO2 类当中
            specVO2.setOptionList(optionsList);
        }
        //最后将规格的list放入到 TemplateVO 当中
        vo.setSpecVO2s(specVO2List);
        return vo;
    }
}
